package com.itheima.util;

import java.util.Vector;
import java.util.Random;
import java.util.TreeSet;

/*
双色球 工具类
 */
public class BallUtil2 {
    private static Random r = new Random();

    public static Vector<Integer> getAllRed() {
        Vector<Integer> list = new Vector<>();
        for (int i = 1; i <= 33; i++) {
            list.add(i);
        }
        return list;
    }

    public static Vector<Integer> getAllBlue() {
        Vector<Integer> list = new Vector<>();
        for (int i = 1; i <= 16; i++) {
            list.add(i);
        }
        return list;
    }

    /**
     * 生成求
     * @param list 原始球
     * @param count 要几个
     * @param result 生成出的球
     */
    public static void getBall(Vector<Integer> list, int count, Vector<Integer> result) {
       //出口 人家不要了
        if (count == 0) return;
        //从已有的求中随一个
        int i = r.nextInt(list.size());
        Integer ball = list.get(i);
        //ball-3
        boolean before1 = getIndex(result, ball - 3);
        //ball-2
        boolean before2 = getIndex(result, ball - 2);
        //ball-1
        boolean before3 = getIndex(result, ball - 1);
        //ball+1
        boolean after1 = getIndex(result, ball +1);
        //ball+2
        boolean after2 = getIndex(result, ball +2);
        //ball+3
        boolean after3 = getIndex(result, ball +3);
        if (before1  && before2 && before3 ) {
           getBall(list,count,result);
        }
        if (before1  && before2  && after1 ) {
            getBall(list,count,result);
        }
        if (before1  && after1  && after2 ) {
            getBall(list,count,result);
        }
        if (after1  && after2  && after3 ) {
            getBall(list,count,result);
        }
        //添加到结果中
        result.add(ball);
        //从原始求中删一个
        list.remove(ball);
        //数量少一个
        count--;
        getBall(list, count, result);
    }

    public static Vector<Integer> getAllBall() {
        Vector<Integer> allRed = getAllRed();
        Vector<Integer> allBlue = getAllBlue();
        Vector<Integer> redResult = new Vector<>();
        Vector<Integer> blueResult = new Vector<>();
        getBall(allRed,6,redResult);
        TreeSet<Integer> redballs = new TreeSet<>(redResult);
        getBall(allBlue,1,blueResult);

        Vector<Integer> allBall = new Vector<Integer>();
        allBall.addAll(redballs);
        allBall.addAll(blueResult);
        return allBall;
    }

    private static boolean getIndex(Vector<Integer> list, int number) {
        if (list == null || list.size() < 0) {
            return false;
        }
        for (Integer i : list) {
            if(i==number){
                return true;
            }
        }
        return false;
    }

}
